window.onload=function(){
    ws = new WebSocket("ws://"+document.domain+":8282");
    ws.onopen = function() {
        // console.log("连接成功！");
        var sendJson = '{"type":"clientId","group":'+admin_id+'}';
        // console.log(sendJson);
        ws.send(sendJson);
    };
    ws.onmessage = function(e) {
        var data = JSON.parse(e.data);
        console.log(data);
        switch(data.type)
        {
            case "clientId":request(data);
                break;
            case "DashboardV2":DashboardV2(data);
                break;
                // default:
                //       n 与 case 1 和 case 2 不同时执行的代码
        }
    };
    
    /**
     * DashboardV2面板基础数据
     *
     */
    function DashboardV2(data) {
        //cpu
        if(data.cpuPercent || data.cpuPercent == 0){
            $('.cpu').text(data.cpuPercent);
        }
        //内存
        if(data.free_memory && data.total_memory){
            var str = data.free_memory+'M/'+data.total_memory+'M'; 
            $('.memory').text(str);
        }
        //在线
        if(data.allCount){
            $('.onLine').text(data.allCount);
        }
        //网络上下行
        if(data.txpck) {
           $('.txpck').text(data.txpck+'kb');
        }
        if(data.rxpck){
            $('.rxpck').text(data.rxpck+'kb');
        }
    }
    /**
     *请求发送数据
     *
     */
    function request(data) {
       var myClientId = data.clientId;
       var url      = document.location.protocol+"//"+document.domain+"/index.php/admin/Pushdata/index";
        $.ajax({
            url:url,
            type:"post",
            data:{type:"DashboardV2",myClientId:myClientId},
            success:function (data){
                console.log(data);
            }
        });
    }
}
